package com.zhiyin.seal.service.impl;

import com.zhiyin.seal.dao.CostDao;
import com.zhiyin.seal.entity.RechargePackageEntity;
import com.zhiyin.seal.entity.RechargeRecordDetailEntity;
import com.zhiyin.seal.entity.RechargeRecordEntity;
import com.zhiyin.seal.entity.bo.RechargePackageBo;
import com.zhiyin.seal.entity.bo.RechargeRecordBo;
import com.zhiyin.seal.entity.bo.RechargeRecordDetailBo;
import com.zhiyin.seal.entity.bo.RechargeRecordWebBo;
import com.zhiyin.seal.service.CostService;
import com.zhiyin.seal.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CostServiceImpl implements CostService {

    @Autowired
    CostDao costDao;

    @Override
    public List<RechargePackageBo> getRechargePackageList() {
        return BeanUtil.copyList(costDao.selectRechargePackageList(),RechargePackageBo.class);
    }

    @Override
    public List<RechargePackageBo> getAllRechargePackageList() {
        return BeanUtil.copyList(costDao.selectAllRechargePackageList(),RechargePackageBo.class);
    }

    @Override
    public RechargePackageBo getRechargePackage(Long packageId) {
        return BeanUtil.copy(costDao.selectRechargePackage(packageId),RechargePackageBo.class);
    }

    @Override
    public Boolean addRechargePackage(RechargePackageBo rechargePackageBo) {
        return costDao.insertRechargePackage(BeanUtil.copy(rechargePackageBo, RechargePackageEntity.class)) > 0;
    }

    @Override
    public Boolean updateRechargePackage(RechargePackageBo rechargePackageBo) {
        return costDao.updateRechargePackage(BeanUtil.copy(rechargePackageBo, RechargePackageEntity.class)) > 0;
    }

    @Override
    public RechargeRecordBo getRechargeRecordById(Long id) {
        RechargeRecordEntity rechargeRecordEntity = costDao.selectRechargeRecordById(id);
        if(rechargeRecordEntity != null){
            return BeanUtil.copy(rechargeRecordEntity,RechargeRecordBo.class);
        }

        return null;
    }

    @Override
    public List<RechargeRecordWebBo> getAllRechargeRecordList() {
        return BeanUtil.copyList(costDao.selectAllRechargeRecordList(),RechargeRecordWebBo.class);
    }

    @Override
    public List<RechargeRecordBo> getRechargeRecordList(Long userId) {
        return BeanUtil.copyList(costDao.selectRechargeRecordList(userId),RechargeRecordBo.class);
    }

    @Override
    public List<RechargeRecordBo> getRechargeRecordListByCompany(Long companyId) {
        return BeanUtil.copyList(costDao.selectRechargeRecordListByCompanyId(companyId),RechargeRecordBo.class);
    }

    @Override
    public List<RechargeRecordDetailBo> getRechargeRecordDetailList(Long recordId) {
        return BeanUtil.copyList(costDao.selectRechargeRecordDetailList(recordId),RechargeRecordDetailBo.class);
    }

    @Override
    public Boolean addOrder(RechargeRecordBo rechargeRecordBo) {
        RechargeRecordEntity rechargeRecordEntity = BeanUtil.copy(rechargeRecordBo,RechargeRecordEntity.class);
        return costDao.insertRechargeRecord(rechargeRecordEntity) > 0;
    }

    @Override
    public Boolean addOrderDetail(List<RechargeRecordDetailBo> rechargeRecordDetailBoList) {
        List<RechargeRecordDetailEntity> rechargeRecordDetailEntityList = BeanUtil.copyList(rechargeRecordDetailBoList,RechargeRecordDetailEntity.class);
        if(rechargeRecordDetailEntityList.size() > 0){
            return costDao.insertRechargeRecordDetail(rechargeRecordDetailEntityList) > 0;
        } else {
            return false;
        }
    }

    @Override
    public RechargeRecordBo queryOrder(String orderNumber) {
        RechargeRecordEntity rechargeRecordEntity = costDao.selectRechargeRecord(orderNumber);
        if(rechargeRecordEntity == null){
            return null;
        }

        return BeanUtil.copy(rechargeRecordEntity,RechargeRecordBo.class);
    }

    @Override
    public Boolean updatePayStatus(String orderNumber, Integer status) {
        return costDao.updateRechargeOrderStatus(orderNumber,status) > 0;
    }

    @Override
    public Boolean updateSealServiceTime(List<RechargeRecordDetailBo> rechargeRecordDetailBoList) {
        List<RechargeRecordDetailEntity> rechargeRecordDetailEntityList = BeanUtil.copyList(rechargeRecordDetailBoList,RechargeRecordDetailEntity.class);
        return costDao.updateSealServiceTime(rechargeRecordDetailEntityList) > 0;
    }
}
